<template>
  <section>
    <el-row>
      <el-col :span="3" class="block">
        <span class="demonstration">默认</span>
      </el-col>
      <el-col :span="10">
        <el-date-picker v-model="date" type="week" format="yyyy 第 WW 周 MM 月 dd 日" placeholder="选择周">
        </el-date-picker>
      </el-col>
    </el-row>
    <el-row v-if="date">
      <el-table  :data="scheduleItemTable" style="width: 100%">
        <el-table-column>
          <el-table-column prop="departmentName" label="医院">
          </el-table-column>
          <el-table-column prop="sectionName" label="科室">
          </el-table-column>
        </el-table-column>
        <el-table-column v-for="(date,i1) in selWeek" :key="date.text" :label="date.text">
          <el-table-column :label="date.day">
            <el-table-column v-for="(ban,i2) in date.bans" :key="ban" :label="ban">
              <template slot-scope="scope">
                <div style="border: 0px solid crimson; min-height: 50px;cursor:pointer">
                  <div v-if="scope.row.repertories[i1*2+i2].length>0">
                    <el-popover v-for="(b,i3) in scope.row.repertories[i1*2+i2]" placement="top-start" trigger="hover" :content='test(scope,i1,i2,i3)' :key="i3">
                      <div slot="reference">{{b.name?b.name:'测试数据'}}</div>
                    </el-popover>
                  </div>
                </div>
              </template>
            </el-table-column>
          </el-table-column>
        </el-table-column>
      </el-table>
    </el-row>
  </section>
</template>

<script>
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  import DateUtils from '@/common/DateUtils'
  import DateFormat from '@/common/date-formater'
  export default {
    name: "getSchedule",
    data() {
      return {
        date:null,
        selWeek:[],
        title:null,
        departments:null,
        scheduleList:null,
        sections:null,
        currSchedule:null,
        scheduleItemTable:null,
        currGoods:null,
        stockList:null
      }
    },
    computed:{
      ...mapGetters(['department']),
    },
    created() {
      this.getHisSectionList()//获取该部门下的科室列表
      this.getScheduleList()//获取该部门下的排班
      this.getStockList()
    },
    watch: {
      date(nv,ov){
        this.getScheduleItemTable()
        let tmp = DateUtils.format(nv, 'yyyy-MM-dd')
        this.title = DateUtils.getWeekNumber(nv)
        this.selWeek.splice(0, this.selWeek.length)
        for (let i = -1; i < 6; i++) {
          let sec = DateUtils.addDays(nv, i)
          let day = DateUtils.getDay(sec)
          let tmp = {text: DateUtils.format(sec, 'yyyy-MM-dd'), day, bans: ['上午', '下午'], index: i}
          this.selWeek.push(tmp)
        }
      },
      title(nv,ov){
        if(nv){
          let tmp = this.scheduleList.find(s => s.title == nv)
          if(tmp){
            this.currSchedule = tmp
            this.getGoodsList(this.currSchedule.id)
            this.getSecheduleItem()
          }
        }
      }
    },
    methods: {
      ...mapActions({
        fetch: 'fetch'
      }),
      getGoodsList(scheduleId){
        console.log(scheduleId)
        this.fetch({name:"getGoodsList",data:{departmentId:this.department.id}})
          .then(res=>{
            if(res.content){
              this.currGoods=res.content.find(g => g.data.scheduleId == scheduleId)//当前排班所生成的商品
            }
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      getStockList(){
        this.fetch({name:"getStockList"})
          .then(res=>{
            console.log(res)
            this.stockList=res
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      test(s,i1,i2,i3){
        if(this.currGoods){
        let goodsSkus = this.currGoods.goodsSkus
        let p = i1*2+i2
        let currSchedule = s.row.repertories[p][i3]//.id
          let sku = goodsSkus.find(s => s.data.scheduleItemId == currSchedule.id)//可以获取当前排班项的商品sku的sn，然后去找库存里的数量
          if(sku.sn){
            //在库存列表里找sn
            let stock = this.stockList.find(s => s.sn == sku.sn)
            console.log(this.stockList)
            let tmp =''
            tmp +=stock.realityStock
            return tmp
          }
        }
      },
      getScheduleItemTable(){
        if(this.sections){
          this.scheduleItemTable = []
          this.pageOne = false
          console.log(this.sections)
          this.sections.forEach(d => { //循环科室列表，做一个空白的排班表
            let data = {
              departmentName: d.departmentName,
              sectionId: d.id,
              sectionName: d.name,
              repertories: [[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
            }
            this.scheduleItemTable.push(data)
          })
        }
        console.log(this.scheduleItemTable)
      },
      getHisSectionList(){
        if(this.department){
          this.fetch({name:"getHisSectionList",data:{departmentId:this.department.id}})
            .then(res=>{
              this.sections = res
            })
            .catch(err=>{
              console.log(err)
              errHandler.handle(this,err)
            })
        }
      },
      getScheduleList(){
        this.fetch({name:"getScheduleList",data:{departmentId:this.department.id,page:0}})
          .then(res=>{
            this.scheduleList = res.content
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      getSecheduleItem(){//获取已有排班项
        let tmp = this.currSchedule.scheduleItems
        let theRep
        for(let i=0;i<tmp.length;i++){
          let section = this.sections.find(d => d.id === tmp[i].section.id)//找到有记录的排班科室
          if (section) {//如果科室存在
            let dIndex = this.scheduleItemTable.findIndex(i => i.sectionId === section.id)//找到这个排班项的索引值
            let theObj = this.scheduleItemTable[dIndex]//找到这个排班项
            //计算14个时间段
            let day = DateUtils.parseDate(DateFormat.format(tmp[i].date)).getDay()
            let time = tmp[i].time === '1' ? 0 : 1
            let colIndex = day * 2 + time
            theRep = theObj.repertories[colIndex]
            //加医生了
            theRep.push({id: tmp[i].id, name: tmp[i].doctorName})
          }
        }
      },
    }
  }
</script>

<style scoped>

</style>
